using System.Web.UI;
using System.Web.UI.Adapters;

using Microsoft.SharePoint;

namespace SPWebPartManager_ControlAdapter
{
    /// <summary>
    /// This ControlAdapter will solve a clientside "Object required" errormessage when 
    /// trying (and fail) to move webparts from WebPartZones. 
    /// Apparently this problem is caused by the style="position:relative" CSS setting.
    /// http://neilmosafi.blogspot.com/2007/11/sharepoint-dragging-webparts-causes.html
    /// </summary>
    /// <remarks>
    /// The Assembly of the control which this ControlAdapter binds to is placed in the GAC.
    /// That assembly should also be placed in the webapplication /bin/-folder. Otherwise a 
    /// "Could not load type" exception will be thrown
    /// </remarks>
    /// <example>
    /// In the "\App_Browsers\compat.browser" file of the webapplication add:
    ///     <browsers>
	///         <browser refID="Default">
    /// 		    <controlAdapters>
    /// 		        <adapter controlType="Microsoft.SharePoint.WebPartPages.SPWebPartManager"
    /// 				         adapterType="MRCL.SPWebPartManager_Adapter, ......" />
	///         	</controlAdapters>
	///         </browser>
    ///     </browsers>
    /// </example>
    public class SPWebPartManager_ControlAdapter : ControlAdapter
    {
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);

            // Write the script directly after the SPWebPartManager output
            writer.WriteLine("<script language=\"javascript\" type=\"text/javascript\">function MSOLayout_GetRealOffset(StartingObject, OffsetType, EndParent) { var realValue=0; if (!EndParent) EndParent=document.body; for (var currentObject=StartingObject; currentObject && currentObject !=EndParent && currentObject != document.body; currentObject=currentObject.offsetParent) { var offset = eval('currentObject.offset'+OffsetType); if (offset) realValue+=offset; } return realValue; } </script>");
        }

    }
}